import groovy.xml.MarkupBuilder

def execute(config) {
	rootDir = new File(config.dir)
	if (!rootDir.exists() || !rootDir.isDirectory()) {
		println "Invalid directory: $rootDir"
	} else {
		def ant = new AntBuilder()
		
		jarsDir = new File("$config.dir${File.separator}WEB-INF${File.separator}lib")
		println "Checking lib directory: $jarsDir"
		if (!jarsDir.exists() || !jarsDir.isDirectory()) {
			println "Lib directory does not exist"
			return
		}
		
		classesDir = new File("$config.dir${File.separator}WEB-INF${File.separator}classes")
		println "Checking classes directory: $classesDir"
		if (!classesDir.exists() || !classesDir.isDirectory()) {
			println "Classes directory does not exist"
			return
		}
		// jar up WDK classes
		classesJarName = 'wdk-classes.jar'
		ant.jar(destfile: classesJarName, basedir: classesDir, includes: 'com/**/*')
		// get list of jars
		jars = []
		jarsDir.eachFileRecurse { jar -> if (jar.name =~ /.*\.jar/) jars << jar }
		// write output files
		pom = writePom(config, jars, classesJarName)
		script = writeScript(config, jars, classesJarName, pom)
		// all done
		println "Completed. Now execute the script '$script'"
	}
}

def writePom(config, files, classesJarName) {
	pom = new File("pom.xml")
	writer = new FileWriter(pom)
	xml = new MarkupBuilder(writer)
	xml.project('xmlns': 'http://maven.apache.org/POM/4.0.0',
				'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
				'xsi:schemaLocation': 'http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd') {
		modelVersion('4.0.0')
		groupId(config.groupId)
		artifactId(config.groupId)
		name(config.groupId)
		version(config.version)
		packaging('pom')
		build {
			pluginManagement {
				plugins {
					plugin {
						groupId(config.groupId)
						artifactId(config.groupId)
						version(config.version)
					}
				}
			}
		}
		dependencies {
			files.each { file ->
				dependency {
					groupId(config.groupId)
					artifactId(file.name[0..-5])
					version(config.version)
				}
			}
			dependency {
				groupId(config.groupId)
				artifactId(classesJarName[0..-5])
				version(config.version)
			}
		}
	}
	pom
}

def writeScript(config, files, classesJarName, pomFile) {
	newline = System.getProperty("line.separator")
	script = new File("publish_wdk.${getFileExtn()}")
	files.each { file ->
		if (config.url) {
			script << "mvn deploy:deploy-file -Dfile=$file -DrepositoryId=$config.repoId -DgroupId=$config.groupId -DartifactId=${file.name[0..-5]} -Dversion=$config.version -Durl=$config.url -Dpackaging=jar -DgeneratePom=true $newline"
		} else {
			script << "mvn install:install-file -Dfile=$file -DgroupId=$config.groupId -DartifactId=${file.name[0..-5]} -Dversion=$config.version -Dpackaging=jar -DgeneratePom=true $newline"
		}
	}
	if (config.url) {
		script << "mvn deploy:deploy-file -Dfile=$classesJarName -DrepositoryId=$config.repoId -DgroupId=$config.groupId -DartifactId=${classesJarName[0..-5]} -Dversion=$config.version -Durl=$config.url -Dpackaging=jar -DgeneratePom=true $newline"
		script << "mvn deploy:deploy-file -Dfile=$pomFile -DrepositoryId=$config.repoId -DgroupId=$config.groupId -DartifactId=$config.groupId -Dversion=$config.version -Durl=$config.url -Dpackaging=pom $newline"
	} else {
		script << "mvn install:install-file -Dfile=$classesJarName -DgroupId=$config.groupId -DartifactId=${classesJarName[0..-5]} -Dversion=$config.version -Dpackaging=jar -DgeneratePom=true $newline"
		script << "mvn install:install-file -Dfile=$pomFile -DgroupId=$config.groupId -DartifactId=$config.groupId -Dversion=$config.version -Dpackaging=pom $newline"
	}
	script
}

def getFileExtn() { isWindows() ? 'cmd' : 'sh' }
def isWindows() { System.getProperty('os.name') ==~ /(?i).*win.*/ }
def usage() { println "Usage: prep_publish_wdk <exploded-war-path> <groupId> <version> [ <repo-url> <repo-id> ]" }

if (args) {
	switch (args.length) {
		case 3:
			execute(dir: args[0], groupId: args[1], version: args[2])
			break
		case 5:
			execute(dir: args[0], groupId: args[1], version: args[2], url: args[3], repoId: args[4])
			break
		default:
			usage()
	}
} else {
	usage()
}

// groovy prep_publish_wdk.groovy /Users/markdav/downloads/documentum/dctm_6.7/da emc.da 6.7.0 http://adserver.markdav.com:8080/nexus/content/repositories/documentum documentum
